package Chazhao;

import java.util.Scanner;

public class Test {

    //掌握二分查找（折半查找）实现原理和编码流程
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找的数：");
        int number = sc.nextInt();
        //定义一个数组
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        //定义两个变量，记录左右位置
        int left = 0;
        int right = arr.length - 1;
        //循环查找数据：循环条件：left <= right
        while (left <= right){
            //定义中间位置
            int mid = (left + right) / 2;
            if (number == arr[mid]){
                System.out.println("找到了索引为：" + mid);
                break;
            }else if (number > arr[mid]){
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
       /* if (left > right){
            System.out.println("没有找到");
        }

        */

    }
}
